package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Company;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class CompanyForm extends BasePage implements Serializable {
    private GenericManager<Company, Long> companyManager;
    private Company company = new Company();
    private Long companyId;

    public void setCompanyManager(GenericManager<Company, Long> companyManager) {
        this.companyManager = companyManager;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    public String delete() {
        companyManager.remove(company.getCompanyId());
        addMessage("company.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (companyId != null && companyId != 0) {
            company = companyManager.get(companyId);
        } else {
            company = new Company();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (company.getCompanyId() == null || company.getCompanyId() == 0);
        companyManager.save(company);

        String key = (isNew) ? "company.added" : "company.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 